package com.sihai.ehr.service.impl;

import com.sihai.core.DataAccessor;
import com.sihai.core.QueryCreator;
import com.sihai.ehr.model.biz.HelpDepartment;
import com.sihai.ehr.service.HelpDepartmentService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * author: wxn
 * since: 13-4-30 上午11:44
 */
@Service("helpDepartmentService")
public class HelpDepartmentServiceImpl implements HelpDepartmentService {
    @Resource
    private DataAccessor dataAccessor;

    @Override
    public void save(HelpDepartment department) throws Exception {
        Assert.notNull(department, "department is required");
        dataAccessor.save(department);
    }

    @Override
    public HelpDepartment get(Long id) throws Exception {
        Assert.notNull(id, "department id is required");
        return dataAccessor.get(HelpDepartment.class, id);
    }

    @Override
    public HelpDepartment findByOrganizationId(final Long organizationId) throws Exception {
        Assert.notNull(organizationId, "organization id is required");
        return dataAccessor.findUnique(HelpDepartment.class, new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                Query query = session.createQuery("from HelpDepartment d where d.organization.id = :oid");
                query.setParameter("oid", organizationId);
                return query;
            }
        });
    }

    @Override
    public boolean delete(Long id) throws Exception {
        dataAccessor.delete(get(id));
        return true;
    }
}
